using UnityEngine;
using Yoozoo.Libs;

namespace Gameplay.PVE.TurnBase
{
    public class RandomMoveAction : SpecialAction
    {

        public bool IsWorking
        {
            get { return state == EActionState.Working; }   
        }
        
        private Vector3 targetPosition;
        private BattleUnit target;
        private float lastActionTime;
        private float actionCd = 5;
        private float speed;
        

        public override void Update()
        {
            base.Update();
            if (unit.Data.IsInAction || unit.Data.velocity != Vector3.zero)
            {
                Reset();
                return;
            }

            if (!IsWorking && TimeManager.logicTime - lastActionTime > actionCd)
            {
                SearchTarget();
                FindRandomPosition();
            }

            if (IsWorking)
            {
                Vector3 diff = targetPosition - unit.Data.position;
                float speed = this.speed * TimeManager.GameDeltaTime;
                if (diff.sqrMagnitude <= speed * speed)
                {
                    unit.Data.SetPosition(targetPosition);
                    unit.Play("Idle");
                    Reset();
                }
                else
                {
                    unit.Data.SetPosition(unit.Data.position + diff.normalized * speed);
                }
            }

            if (target != null)
            {
                Vector3 forward = target.Data.position - unit.Data.position;
                forward.y = 0;
                unit.Data.SetForward(forward);
            }
            
        }

        private void SearchTarget()
        {
            if (target != null && !target.Data.isDead)
            {
                return;
            }
            int team = unit.Data.GetTargetTeam();
            var candidates = TurnBaseManager.Instance.GetAllUnitByTeam(team);
            if (candidates.Count > 0)
            {
                candidates.Sort(SortDistance);
                target = candidates[0];
            }
        }

        private void FindRandomPosition()
        {
            if (target == null)
            {
                return;
            }

            Vector3 forward = unit.Data.position - target.Data.position;
            float dir = Random.Range(0, 1f) < 0.5f ? 1 : -1;
            float angle = dir * Random.Range(20,40f);
            Vector3 towards = Quaternion.AngleAxis(angle, Vector3.up) * forward;
            float min = 0.7f;
            float max = Mathf.Max(1, Vector3.Distance(target.Data.position, unit.Data.position));
            float distance = Random.Range(min, max);
            float ratio = Random.Range(0.8f,1.2f);
            speed = ratio * 0.1f;
            targetPosition = target.Data.position + towards.normalized * distance;

            var fromBirthPosition = targetPosition - unit.Data.birthPosition;
            float maxRange = 0.3f;
            if (fromBirthPosition.magnitude > maxRange)
            {
                targetPosition = unit.Data.birthPosition + fromBirthPosition.normalized * maxRange;
            }
            state = EActionState.Working;
            unit.Play("Walk",0.4f * ratio);
        }

        private int SortDistance(BattleUnit a,BattleUnit b)
        {
            float dist1 = (unit.Data.position - a.Data.position).sqrMagnitude;
            float dist2 = (unit.Data.position - b.Data.position).sqrMagnitude;
            return dist1.CompareTo(dist2);
        }
        
        public override void Reset()
        {
            base.Reset();
            actionCd = Random.Range(5f, 20f);
            lastActionTime = TimeManager.logicTime;
        }
    }
}